package com.pzh.assignment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzh.assignment.common.bean.Result;
import com.pzh.assignment.common.bean.param.CourseParam;
import com.pzh.assignment.common.bean.vo.CourseVO;
import com.pzh.assignment.entity.Course;
import com.pzh.assignment.service.ICourseService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pengzhihao
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    ICourseService courseService;

    @RequiresRoles(value = {"sys_admin", "admin"}, logical = Logical.OR)
    @GetMapping
    public Result listCourseByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                  @RequestParam(defaultValue = "5") Integer size) {
        Page<Course> page = new Page<>(currentPage, size);
        IPage pageData = courseService.page(page, new QueryWrapper<Course>().orderByDesc("create_time"));
        List<CourseVO> voList = ((IPage<Course>) pageData).getRecords().stream().map(CourseVO::valueOf).collect(Collectors.toList());
        pageData.setRecords(voList);
        return Result.success(pageData);
    }

    @GetMapping("/total")
    public Result totalCourse() {
        List<Course> courses = courseService.list();
        List<CourseVO> voList = courses.stream().map(CourseVO::valueOf).collect(Collectors.toList());
        return Result.success(voList);
    }

    @RequiresRoles(value = {"sys_admin", "admin"}, logical = Logical.OR)
    @PostMapping
    public Result saveOrUpdate(@RequestBody CourseParam param) {
        Course course = new Course();
        course.setName(param.getName());
        course.setCode(param.getCode());
        course.setTypeId(param.getTypeId());
        if (param.getId() != null) {
            course.setId(param.getId());
        }
        courseService.saveOrUpdate(course);
        return Result.success(null);
    }

    @RequiresRoles(value = {"sys_admin", "admin"}, logical = Logical.OR)
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        courseService.removeById(id);
        return Result.success(null);
    }

}
